package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OtherProductDTO;
import com.sky.dto.OtherProductPageQueryDTO;
import com.sky.entity.OtherProduct;
import com.sky.mapper.OtherProductMapper;
import com.sky.result.PageResult;
import com.sky.service.OtherProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class OtherProductServiceImpl implements OtherProductService {
    
    @Autowired
    private OtherProductMapper otherProductMapper;
    
    @Override
    public void save(OtherProductDTO otherProductDTO) {
        OtherProduct otherProduct = new OtherProduct();
        BeanUtils.copyProperties(otherProductDTO, otherProduct);
        
        otherProduct.setCreateTime(LocalDateTime.now());
        otherProduct.setUpdateTime(LocalDateTime.now());
        
        otherProductMapper.insert(otherProduct);
    }
    
    @Override
    public PageResult pageQuery(OtherProductPageQueryDTO otherProductPageQueryDTO) {
        PageHelper.startPage(otherProductPageQueryDTO.getPage(), otherProductPageQueryDTO.getPageSize());
        Page<OtherProduct> page = otherProductMapper.pageQuery(otherProductPageQueryDTO);
        
        return new PageResult(page.getTotal(), page.getResult());
    }
    
    @Override
    public void update(Long id, OtherProductDTO otherProductDTO) {
        OtherProduct otherProduct = new OtherProduct();
        BeanUtils.copyProperties(otherProductDTO, otherProduct);
        
        otherProduct.setId(id);
        otherProduct.setUpdateTime(LocalDateTime.now());
        
        otherProductMapper.update(otherProduct);
    }
    
    @Override
    public void delete(Long id) {
        otherProductMapper.deleteById(id);
    }
}